<?php
/**
 * Created by yb.
 * Author: 1021948372@qq.com
 * Date: 2020/7/7 07:07
 */
namespace app\common\controller;

use app\common\lib\SessionCheck;

class AdminMenu extends SessionCheck
{
    /**
     * 列表
     * @return mixed
     */
    public function index()
    {
        $this->view->engine->layout('common@tree_layout');
        return $this->fetch('common@admin_menu/index');
    }

    /**
     * 权限tree数据
     */
    public function getMenuList(){
        $menuList = db('admin_menu')->field('id,pid,name,path,if(status,"启用","禁用") status,if(is_show,"展示","隐藏") is_show')->where(array('module' => request()->module()))->order('pid asc,sort desc')->select();
        $result = array(
            'msg' => '权限列表',
            'code' => 0,
            'data' => $menuList,
            'count' => count($menuList),
            'is' => true,
            'tip' => '获取成功'
        );
        echo json_encode($result);
    }

    /**
     * 添加
     * @return mixed
     */
    public function add(){
        if(request()->isPost()){
            if(empty(input('name'))){
                $this->error('请填写名称');
            }
            $saveData = array(
                'pid' => input('pid'),
                'module' => request()->module(),
                'name' => input('name'),
                'path' => input('path',''),
                'level' => input('pid') > 0 ? 2 : 1,
                'status' => input('status'),
                'is_show' => input('is_show'),
                'icon' => input('icon',''),
                'sort' => input('sort',50),
                'create_time' => time()
            );
            $model = new \app\common\model\AdminMenu();
            if($model->insert($saveData)){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{
            $firstMenuList = db('admin_menu')->where(array('pid' => 0,'module' => request()->module()))->select();
            $this->assign('firstMenuList',$firstMenuList);
            return $this->fetch('common@admin_menu/add');
        }
    }

    /**
     * 修改
     * @return mixed
     */
    public function edit(){
        if(request()->isPost()){
            if(empty(input('name'))){
                $this->error('请填写名称');
            }
            $id = input('id');
            $saveData = array(
                'pid' => input('pid'),
                'name' => input('name'),
                'path' => input('path',''),
                'level' => input('pid') > 0 ? 2 : 1,
                'status' => input('status'),
                'is_show' => input('is_show'),
                'icon' => input('icon',''),
                'sort' => input('sort',50)
            );
            $model = new \app\common\model\AdminMenu();
            if($model->save($saveData,['id' => $id])){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{
            $id = input('id');
            $data = db('admin_menu')->where(array('id' => $id))->find();
            $this->assign('data',$data);
            $firstMenuList = db('admin_menu')->where(array('pid' => 0,'module' => request()->module()))->select();
            $this->assign('firstMenuList',$firstMenuList);
            return $this->fetch('common@admin_menu/edit');
        }
    }

    /**
     * 删除
     */
    public function del(){
        $id = input('id');
        $count = db('admin_menu')->where(array('pid' => $id))->count();
        if($count){
            $this->error('权限下有子权限，请先删除子权限再删除父级权限');
        }
        $model = new \app\common\model\AdminMenu();
        if($model->where(array('id' => $id))->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

}